﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SPN.WebParts.Confirmation
{
    public partial class ConfirmationUserControl : UserControl
    {
        #region Properties - Labels
        public string _Settings_WebUrl { get; set; }
        public string _Label_SubscribeMessage { get; set; }
        public string _Label_AlreadySubscribeMessage { get; set; }
        public string _Label_UnSubscribeMessage { get; set; }
        public string _Label_CodeNotValid { get; set; }
        public string _Label_EmailNotValid { get; set; }
        #endregion

        #region PageEvents
        protected void Page_Load(object sender, EventArgs e)
        {
            CssRegistration.Register("/_layouts/SPN/css/main.css");
            if (_Settings_WebUrl != "")
            {
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    using (SPSite _Site = new SPSite(SPContext.Current.Site.ID))
                    {
                        SPWeb _TestWeb = null;
                        try
                        {
                            _TestWeb = _Site.AllWebs[_Settings_WebUrl];
                            string _UrlValue = _TestWeb.ServerRelativeUrl;
                        }
                        catch
                        {
                            _TestWeb = null;
                        }
                        if (_TestWeb != null)
                        {
                            using (SPWeb _Web = _Site.AllWebs[_TestWeb.ID])
                            {
                                string _ConfirmationUrl = Common.Helper.GetWebProperty(_Web, Common.Helper._SPN_ConfirmationUrl);

                                if (Common.Helper.IsListValid(_Web, Common.Helper._Const_SubscriberListName) && Common.Helper.IsListValid(_Web, Common.Helper._Const_NewsletterListName))
                                {
                                    if (Page.Request.QueryString["email"] != null && Page.Request.QueryString["code"] != null && Page.Request.QueryString["action"] != null)
                                    {
                                        string _Email = Page.Request.QueryString["email"].ToString();
                                        string _Code = Page.Request.QueryString["code"].ToString();
                                        string _Action = Page.Request.QueryString["action"].ToString();

                                        SPList _List = _Web.Lists[Common.Helper._Const_SubscriberListName];
                                        SPListItemCollection _Subscribers = _List.Items;
                                        SPListItem _Subscriber = null;
                                        for (int i = 0; i < _Subscribers.Count; i++)
                                        {
                                            SPListItem _Temp_Subscriber = _Subscribers[i];
                                            if (_Temp_Subscriber["Title"].ToString() == _Email)
                                            {
                                                _Subscriber = _Temp_Subscriber;
                                            }
                                        }
                                        if (_Subscriber["Activation code"].ToString() == _Code)
                                        {
                                            if (_Action == "subscribe")
                                            {
                                                bool _Active = Convert.ToBoolean(_Subscriber["Active"].ToString());
                                                if (!_Active)
                                                {
                                                    _Subscriber["Active"] = true;
                                                    _Web.AllowUnsafeUpdates = true;
                                                    _Subscriber.Update();
                                                    _Web.AllowUnsafeUpdates = false;
                                                    lab_Message.Text = _Label_SubscribeMessage;
                                                }
                                                else
                                                {
                                                    lab_Message.Text = _Label_AlreadySubscribeMessage;
                                                }
                                            }
                                            else if (_Action == "unsubscribe")
                                            {
                                                _Web.AllowUnsafeUpdates = true;
                                                _Subscriber.Delete();
                                                _Web.AllowUnsafeUpdates = false;
                                                lab_Message.Text = _Label_UnSubscribeMessage;
                                            }
                                        }
                                        else
                                        {
                                            lab_Message.Text = _Label_CodeNotValid;
                                        }
                                    }
                                }
                            }
                        }
                        else { lab_Message.Text = "Entered WebUrl is not valid."; }
                    }
                });
            }
            else { lab_Message.Text = "Web Part is not configured. WebUrl is missing."; }
        }
        #endregion
    }
}
